home *** CD-ROM | disk | FTP | other *** search
- Path: news.interlog.com!news
- From: willer@interlog.com (Steve Willer)
- Newsgroups: comp.lang.c++
- Subject: Re: auto_ptr capability question
- Date: Fri, 05 Apr 1996 20:26:52 GMT
- Organization: InterLog Internet Services
- Message-ID: <316577c3.392060888@news.interlog.com>
- References: <31600aa3.1288774@news.xmission.com> <31630583.85660528@news.xmission.com>
- NNTP-Posting-Host: ip93-211.tor.interlog.com
- X-Newsreader: Forte Agent .99d/32.182
-
- macron@xmission.com (Joe Schlimgen) wrote:
-
- >True, but a minor detail that has nothing to do with the problem...
-
- I know. I just wanted to be sure you were clear on it.
-
- >I agree that a few extra keystrokes that avoid possible problems are
- >well worth it. I've also been using my auto_ptr-like class and a string
- >class that have implicit conversions with no problems (unless they're
- >**very** subtle). Mostly, the implicit conversions have been used for
- >parameters to older functions. Can you give any examples of the type of
- >problems you're talking about?
-
- From what I remember, the problem isn't that it will introduce many subtle bugs
- into your code. In fact, the problem is that you might get conversions at times
- when you don't want one. I wish I could give you an example, but I don't
- remember any and I don't have any of my programming books at home (it's a
- holiday here in Canada today). If no one's posted one by Monday, I'll send you
- one.
-
- Alternatively, you could go out to a bookstore and get "Effective C++" and
- "More Effective C++". I'm telling you, it's helped me to clean up potential
- errors in my coding.
-
- >I've always understood the transfer of ownership. Let me change your
- >example just a bit to show the problem I'm talking about:
- >
- >auto_ptr<T> a(new T); // Given 'a' something to manage
- >auto_ptr<T> b(new T);
- >a = b;
-
- I took another look at your auto_ptr class. It looks like your reset() function
- isn't right. It should be deleting the pointer, then setting it to the
- argument. Here's the auto_ptr I use, which is included in Meyers's book but
- adapted so it will compile in Borland C++ 4.5 (which doesn't have member
- templates or the explicit keyword):
-
- // This "auto_ptr" is a smart pointer class that is part of the latest WP
- // and should be in BC5.0. This particular implementation was taken from
- // "More Effective C++" by Meyers, page 293
- template <class T> class auto_ptr {
- public:
- /* explicit */ auto_ptr(T *p=0): pointee(p) {}
- // template<class U> auto_ptr(auto_ptr<U> &rhs): pointee(rhs.release()) {}
- auto_ptr(auto_ptr<T> &rhs): pointee(rhs.release()) {}
- ~auto_ptr() {delete pointee;}
- // template<class U> auto_ptr<T>& operator=(auto_ptr<U> &rhs) {
- auto_ptr<T> &operator=(auto_ptr<T> &rhs) {
- if (this != &rhs) reset(rhs.release());
- return *this;
- }
- T& operator*() const {return *pointee;}
- T* operator->() const {return pointee;}
- T* get() const {return pointee;}
- T* release() {
- T *oldPointee = pointee;
- pointee = 0;
- return oldPointee;
- }
- void reset(T *p=0) {delete pointee; pointee = p;}
- private:
- T *pointee;
- };
-
- >auto_ptr<T> a(new T);
- >auto_ptr<T> b(new T);
- >a = b;
-
- Now, on the "a=b" line, the second T would get deleted, "a" would point to
- nothing, and "b" would point to the first T.
-
- >(Talk about subtle bugs...) This places the responsibility of deleting
- >the previous object on the programmer. And you know how responsible we
- >are. :)
-
- Oh yes. Yes, I do.
-